<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="模块名称" prop="moduleName">
      <el-input v-model="dataForm.moduleName" placeholder="模块名称"></el-input>
    </el-form-item>
    <el-form-item label="排序序号" prop="orderNumber">
      <el-input v-model="dataForm.orderNumber" placeholder="排序序号"></el-input>
    </el-form-item>
    <el-form-item label="父级项目id" prop="projectId">
      <el-input v-model="dataForm.projectId" placeholder="父级项目id"></el-input>
    </el-form-item>
    <el-form-item label="" prop="creatorId">
      <el-input v-model="dataForm.creatorId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="creatorName">
      <el-input v-model="dataForm.creatorName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createdTime">
      <el-input v-model="dataForm.createdTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="modifierId">
      <el-input v-model="dataForm.modifierId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="modifierName">
      <el-input v-model="dataForm.modifierName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="modifiedTime">
      <el-input v-model="dataForm.modifiedTime" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          moduleName: '',
          orderNumber: '',
          projectId: '',
          creatorId: '',
          creatorName: '',
          createdTime: '',
          modifierId: '',
          modifierName: '',
          modifiedTime: ''
        },
        dataRule: {
          moduleName: [
            { required: true, message: '模块名称不能为空', trigger: 'blur' }
          ],
          orderNumber: [
            { required: true, message: '排序序号不能为空', trigger: 'blur' }
          ],
          projectId: [
            { required: true, message: '父级项目id不能为空', trigger: 'blur' }
          ],
          creatorId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          creatorName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createdTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modifierId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modifierName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modifiedTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/report/module/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.moduleName = data.module.moduleName
                this.dataForm.orderNumber = data.module.orderNumber
                this.dataForm.projectId = data.module.projectId
                this.dataForm.creatorId = data.module.creatorId
                this.dataForm.creatorName = data.module.creatorName
                this.dataForm.createdTime = data.module.createdTime
                this.dataForm.modifierId = data.module.modifierId
                this.dataForm.modifierName = data.module.modifierName
                this.dataForm.modifiedTime = data.module.modifiedTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/report/module/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'moduleName': this.dataForm.moduleName,
                'orderNumber': this.dataForm.orderNumber,
                'projectId': this.dataForm.projectId,
                'creatorId': this.dataForm.creatorId,
                'creatorName': this.dataForm.creatorName,
                'createdTime': this.dataForm.createdTime,
                'modifierId': this.dataForm.modifierId,
                'modifierName': this.dataForm.modifierName,
                'modifiedTime': this.dataForm.modifiedTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
